<?php
/**
 * Created by PhpStorm.
 * User: h
 * Date: 2021/4/8
 * Time: 16:05
 */

namespace app\api\controller\v3;
use app\common\model\Attribute;
use app\common\model\Product;
use app\common\model\ProductCate;
use app\common\model\Value;

class Pro extends BaseController
{
    public function getInfo()
    {

        $type = input('type')?input('type'):'id';
        $sort = input('sort')?input('sort'):'desc';
        $cate_id = input('cate_id');
        $page = input('page')?input('page'):1;
        $value = input('value_id');


        if (empty($cate_id)) {
            return errMsg(500, '请传入分类id');
        }
        $cate = ProductCate::where('id', $cate_id)
            ->field('id,name,pid')
            ->select()
            ->toArray();
        if($cate[0]['pid'] != 0){   //不是顶层
            //获取顶层
            $cate = ProductCate::where('id', $cate[0]['pid'])
                ->field('id,name,pid')
                ->select()
                ->toArray();
            //把同层的找出来
            $cate_child = ProductCate::where('pid', $cate[0]['id'])
                ->field('id,name,pid')
                ->select()
                ->toArray();
        }else{
            //子层
            $cate_child = ProductCate::where('pid', $cate_id)
                ->field('id,name,pid')
                ->select()
                ->toArray();
            $cate_id = $cate_child!=[]?$cate_child[0]['id']:$cate_id;   //如果有子层  查子层的第一个
        }
        $pro = Product::where('cate_id',$cate_id)
            ->field('id,value_id')
            ->select()
            ->toArray();   //需要
        $value_id = implode(',', array_column($pro, 'value_id'));    //获取所有的组合
        $attr = Value::alias('a')
            ->join('br_product_attribute b', 'a.a_id = b.id')
            ->where('a.id', 'in', $value_id)
            ->field('a.name,b.name attr_name,a.id value_id')
            ->select()
            ->toArray();
        $attrs = [];   //筛选
        if($attr){
            foreach ($attr as $k => &$v) {
                $attrs[$v['attr_name']][] = $v;          //需要
            }
        }
        if(empty($value)){   //为空 不带搜索条件
            $product =Product::where('cate_id',$cate_id)
            ->field('id,name,tags,brand,big_image,price,value_id')
                ->limit(($page-1)*10,10)
                ->order($type,$sort)
                ->select()
                ->toArray();   //需要           //获取产品
            $count =Product::where('cate_id',$cate_id)
                ->field('id')
                ->count();
            $pagemax = intval(ceil($count/10));    //分页最大页码
        }else{    //有搜索添加
            $data = $this->getPro($cate_id,$value,$page,$type,$sort);
            $product = $data['product'];
            $pagemax = $data['pagemax'];
        }
        $value_all = Value::alias('a')
            ->join('br_product_attribute b', 'a.a_id = b.id')
            ->where('a.id', 'in', $value)
            ->field('a.name,b.name attr_name,a.id value_id')
            ->select()
            ->toArray();//获取搜索条件

        $values_tiao = [];   //筛选
        if($value_all){
            foreach ($value_all as $k => &$v) {
                $values_tiao[$v['attr_name']][] = $v;          //需要
            }
        }
        return sucessMsg(200,'请求成功',
            [
                'cate_id'=>$cate_id,
                'cate'=>$cate,
                'cate_child'=>$cate_child,
                'attrs'=>$attrs,
                'pro'=>$product,
                'pagemax'=>$pagemax,
                'values'=>$values_tiao,
            ]);
    }
    public function getPro($cate_id,$value,$page,$type,$sort)
    {
        foreach ($value as $v) {
            $res[] = Product::where('cate_id',$cate_id)
                ->whereFindInSet('value_id', $v)
                ->field('id,name,tags,brand,big_image,price,value_id')
                ->order($type,$sort)
                ->select()
                ->toArray();
        }
        $result = array_reduce($res, 'array_merge', array());
        $array=[];
        foreach($result as $key=>$v){
            if(!in_array($v,$array)){
                $array[]=$v;
            }
        }
        foreach ($array as $k=>&$v){
            foreach ($value as $vv){
                if(!in_array($vv,explode(',',$v['value_id']))){
                    unset($array[$k]);
                }
            }
            unset($v);
        }
        $product = array_values($array);
        $count = count($product);
        $pageMax = intval(ceil($count/10));
        $product=array_slice($product,($page-1)*10,10);
        return ['pagemax'=>$pageMax,'product'=>$product];

    }
}